﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using Sendz.Model;
using Sendz.WebUI.Infrastructure;
using Ninject;

namespace Sendz.WebUI.Authorization
{
    public class CustomRoleProvider : RoleProvider
    {
        #region Repository injection //////////////////////////////////

        // Aggregate repositories, injected by container factory in /Infrastructure
        // Use alternate injection pattern with NinjectHelper (see notes in NinjectControllerFactory)
        private IUserRepository dbUser;

        public CustomRoleProvider()
        {
            NinjectHelper.Kernel.Inject(this);
        }

        [Inject]
        public IUserRepository Repository 
        { 
            set
            {
                dbUser = value;
            }
        }

        #endregion

        public override string[] GetRolesForUser(string username)
        {
            var user = dbUser.GetUserByName(username);
            List<string> roles = new List<string>();
            // Everyone's a user, this is basic!
            roles.Add("User");
            if (user.IsAdmin)
                roles.Add("Admin");
            return roles.ToArray();
        }

        #region Not implemented //////////////////////////////////

        public override string ApplicationName { 
            get { throw new NotImplementedException(); } 
            set { throw new NotImplementedException(); } 
        }       
        
        public override void AddUsersToRoles(string[] usernames, string[] roleNames) 
        { 
            throw new NotImplementedException(); 
        }       
        
        public override void RemoveUsersFromRoles(string[] usernames, string[] roleNames) 
        { 
            throw new NotImplementedException(); 
        }       
        
        public override void CreateRole(string roleName) 
        { 
            throw new NotImplementedException(); 
        }       
        
        public override bool DeleteRole(string roleName, bool throwOnPopulatedRole) 
        { 
            throw new NotImplementedException(); 
        }       
        
        public override bool RoleExists(string roleName) 
        { 
            throw new NotImplementedException(); 
        }                 
        
        public override string[] GetUsersInRole(string roleName) 
        { 
            throw new NotImplementedException(); 
        }       
        
        public override string[] FindUsersInRole(string roleName, string usernameToMatch) 
        { 
            throw new NotImplementedException(); 
        }       
        
        public override string[] GetAllRoles() 
        { 
            throw new NotImplementedException(); 
        }

        public override bool IsUserInRole(string username, string roleName)
        {
            throw new NotImplementedException();
        }

        #endregion
    }
}